//
//  foundQuestBGTableViewCell.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/8/2.
//

#import "foundQuestBGTableViewCell.h"
#import "foundQuestModel.h"
#import "YTBaseSelectStringView.h"
#import "YTBaseEditorStringView.h"
#import "YTSelectStringShowView.h"
#import "DigitalInputView.h"

@interface foundQuestBGTableViewCell ()<UITextFieldDelegate>

@property (nonatomic ,strong) UIView * backview;

@property (nonatomic ,strong) UIView * oneview;
@property (nonatomic ,strong) UIView * oneLab;
@property (nonatomic ,strong) UIView * tweview;
@property (nonatomic ,strong) UIView * threeview;
@property (nonatomic ,strong) UITextField * tweTextField;
@property (nonatomic ,strong) UITextField * threeTextField;

@end
@implementation foundQuestBGTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        [self setupView];
    }
    return self;
}


- (void)setupView{
    
    self.backgroundColor = UIColor.clearColor;
    self.contentView.backgroundColor = UIColor.whiteColor;
    self.userInteractionEnabled = YES;
    self.contentView.userInteractionEnabled = YES;
    
    UIView * backview = [UIView new];
    [self addSubview:backview];
    [backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.offset(0);
        make.right.offset(kwt(-10));
        make.left.offset(kwt(10));
    }];
    
  
    
    UIView * oneview = [UIView new];
    self.oneview = oneview;
    [backview addSubview:oneview];
    [oneview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.offset(0);
        make.width.offset(kwt(93));
    }];
    
    UIView * line = [UIView new];
    [self addSubview:line];
    line.backgroundColor = KcolorHex(@"#D5D5D5");
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.equalTo(oneview);
        make.width.offset(0.5);
    }];
    UIView * tweview = [UIView new];
    self.tweview = tweview;
    [backview addSubview:tweview];
    [tweview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.offset(0);
        make.width.offset(kwt(93));
        make.left.equalTo(oneview.mas_right);
    }];
    
    
    UIView * line1 = [UIView new];
    [self addSubview:line1];
    line1.backgroundColor = KcolorHex(@"#D5D5D5");
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.equalTo(tweview);
        make.width.offset(0.5);
    }];
    
    UIView * threeview = [UIView new];
    self.threeview = threeview;
    [backview addSubview:threeview];
    [threeview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.bottom.offset(0);
        make.left.equalTo(tweview.mas_right);
    }];
    
    UIView * topline = [UIView new];
    [backview addSubview:topline];
    topline.backgroundColor = KcolorHex(@"#D5D5D5");
    [topline mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.offset(0);
        make.height.offset(0.5);
    }];
    
    UIView * leftline = [UIView new];
    [backview addSubview:leftline];
    leftline.backgroundColor = KcolorHex(@"#D5D5D5");
    [leftline mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.offset(0);
        make.width.offset(0.5);
    }];
    
    UIView * rightline = [UIView new];
    [backview addSubview:rightline];
    rightline.backgroundColor = KcolorHex(@"#D5D5D5");
    [rightline mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.offset(0);
        make.width.offset(0.5);
    }];
}

- (void)setModel:(pondModelListModel *)model{
    _model = model;
    [self.oneview.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self.tweview.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self.threeview.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    MJWeakSelf
    UILabel * onelab = [DTKTool labelWithText:model.name andFont:kFont(15) andTextColor:KcolorHex(@"#222222")];
    [self.oneview addSubview:onelab];
    onelab.numberOfLines = 2;
    [onelab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.offset(0);
        make.left.offset(kwt(6));
        make.right.offset(kwt(-6));
    }];
    
    if(model.taskItemModelList.firstObject.pattern == 2){///单选
        YTMinSelectStringView * view = [YTMinSelectStringView NewView];
        [self.tweview addSubview:view];
        view.content = model.taskItemModelList.firstObject.itemSeledModel.name;
        view.block = ^(NSString * _Nonnull content) {
            if(weakSelf.openXLDXBlock){
                weakSelf.openXLDXBlock(model.taskItemModelList.firstObject,model.taskItemModelList.firstObject.itemSeledModel,model.taskItemModelList.lastObject.name);
            }
        };
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.bottom.offset(0);
        }];
    } else  if(model.taskItemModelList.firstObject.pattern == 3){///数字输入
        
        DigitalInputView * view = [DigitalInputView NewView];
        [self.tweview addSubview:view];
        view.model = model.taskItemModelList.firstObject;
        view.openHUDBlock = weakSelf.openHUDBlock;
        view.openReloadBlock = weakSelf.ReloadBlock;
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.bottom.offset(0);
        }];
        
    } else  if(model.taskItemModelList.firstObject.pattern == 4 || model.taskItemModelList.firstObject.pattern == 5){///文本输入
        
        UITextField * tweTextField = [UITextField new];
        tweTextField.delegate = self;
        tweTextField.placeholder = @"请填写";
        tweTextField.text = model.taskItemModelList.firstObject.content;
        tweTextField.attributedPlaceholder = [DTKTool AttributedStringWithStr:[NSString stringWithFormat:@"请填写"] andNodeColor:KcolorHex(@"#787A79") andSeledColor:KcolorHex(@"#787A79") andFont:kFont(18) andPattenr:@""];
        tweTextField.font = kFont(18);
        tweTextField.textColor = KcolorHex(@"#222222");
        tweTextField.textAlignment = NSTextAlignmentRight;
        self.tweTextField = tweTextField;
        [self.tweview addSubview:tweTextField];
        [tweTextField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.offset(0);
            make.left.offset(kwt(6));
            make.right.offset(kwt(-6));
        }];
    }
    
    if(model.taskItemModelList.count > 1){///2个任务
        
        [self.oneview mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.offset(kwt(93));
        }];
        [self.tweview mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.offset(kwt(131));
        }];
        
        
        if(model.taskItemModelList.lastObject.pattern == 2){///单选
            YTMinSelectStringView * view = [YTMinSelectStringView NewView];
            [self.threeview addSubview:view];
            view.content = model.taskItemModelList.lastObject.itemSeledModel.name;
            view.block = ^(NSString * _Nonnull content) {
//                for (TemplateFoundDetailsItemModelListModel * item in model.taskItemModelList.lastObject.itemModelList) {
//                    if([item.name isEqualToString:content]){
//                        item.checkedFlag = 1;
//                    } else {
//                        item.checkedFlag = 0;
//                    }
//                }
//                if(weakSelf.ReloadBlock){
//                    weakSelf.ReloadBlock();
//                }
                if(weakSelf.openXLDXBlock){
                    weakSelf.openXLDXBlock(model.taskItemModelList.lastObject,model.taskItemModelList.lastObject.itemSeledModel,model.taskItemModelList.lastObject.name);
                }
            };
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.top.bottom.offset(0);
            }];
        } else  if(model.taskItemModelList.lastObject.pattern == 3){///数字输入
            
            DigitalInputView * view = [DigitalInputView NewView];
            [self.threeview addSubview:view];
            view.model = model.taskItemModelList.lastObject;
            view.openHUDBlock = weakSelf.openHUDBlock;
            view.openReloadBlock = weakSelf.ReloadBlock;
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.top.bottom.offset(0);
            }];
            
        } else  if(model.taskItemModelList.lastObject.pattern == 4 || model.taskItemModelList.lastObject.pattern == 5){///文本输入
            
            UITextField * threeTextField = [UITextField new];
            threeTextField.delegate = self;
            threeTextField.placeholder = @"请填写";
            threeTextField.text = model.taskItemModelList.lastObject.content;
            threeTextField.attributedPlaceholder = [DTKTool AttributedStringWithStr:[NSString stringWithFormat:@"请填写"] andNodeColor:KcolorHex(@"#787A79") andSeledColor:KcolorHex(@"#787A79") andFont:kFont(18) andPattenr:@""];
            threeTextField.font = kFont(18);
            threeTextField.textColor = KcolorHex(@"#222222");
            threeTextField.textAlignment = NSTextAlignmentRight;
            self.threeTextField = threeTextField;
            [self.threeview addSubview:threeTextField];
            [threeTextField mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.bottom.offset(0);
                make.left.offset(kwt(6));
                make.right.offset(kwt(-6));
            }];
        }
        
    } else {///1个任务
        [self.oneview mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.offset(kwt(177.5));
        }];
        [self.tweview mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.offset(kwt(177.5));
        }];
        
    }
}


-(void)textFieldDidEndEditing:(UITextField *)textField{
    if(self.tweTextField == textField){
        self.model.taskItemModelList.firstObject.content = textField.text;
    } else if(self.threeTextField == textField){
        self.model.taskItemModelList.lastObject.content = textField.text;
    }
}
@end
